//
//  COptionView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 12-1-1.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "COptionView.h"

//======================================================================================================================
@implementation CSwitch

//@synthesize m_LeftBtn;
//@synthesize m_RightBtn;
//@synthesize m_On;

@synthesize m_Switch;
@synthesize m_Target;
@synthesize m_Action;

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        /*UIFont *font = [UIFont fontWithName:@"Helvetica" size:13];
        
        m_LeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [m_LeftBtn setTitle:@"NO" forState:UIControlStateNormal];
        [m_LeftBtn setBackgroundImage:[UIImage imageNamed:@"swleft.png"] forState:UIControlStateNormal];
        m_LeftBtn.frame = CGRectMake(0, 0, frame.size.width/2, frame.size.height);
        m_LeftBtn.enabled = YES;
        m_LeftBtn.titleLabel.textColor = [UIColor blackColor];
        m_LeftBtn.tag = 0;
        m_LeftBtn.titleLabel.font = font;
        [m_LeftBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_LeftBtn];
        
        m_RightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [m_RightBtn setTitle:@"OFF" forState:UIControlStateNormal];
        [m_RightBtn setBackgroundImage:[UIImage imageNamed:@"swright.png"] forState:UIControlStateNormal];
        m_RightBtn.frame = CGRectMake(frame.size.width/2, 0, frame.size.width/2, frame.size.height);
        m_RightBtn.tag = 1;
        m_RightBtn.titleLabel.font = font;
        m_RightBtn.enabled = NO;
        [m_RightBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_RightBtn];*/
        
        m_Switch = [[UISwitch alloc] init];
        [m_Switch addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventValueChanged];
        m_Switch.center = CGPointMake(m_Switch.center.x, self.frame.size.height/2);
        [self addSubview:m_Switch];
        [m_Switch release];
        
        //m_On = NO;
    }
    
    return self;
}

-(void)OnButtonDown:(UIButton*)sender
{
    /*if (1 == sender.tag)
    {
        m_RightBtn.enabled = NO;
        m_LeftBtn.enabled  = YES;
        
        m_LeftBtn.titleLabel.textColor  = [UIColor blackColor];
        //m_RightBtn.titleLabel.textColor = [UIColor grayColor];
        
        m_On = NO;
    }
    else if (0 == sender.tag)
    {
        m_RightBtn.enabled = YES;
        m_LeftBtn.enabled  = NO;
        
        m_RightBtn.titleLabel.textColor = [UIColor blackColor];
        //m_LeftBtn.titleLabel.textColor  = [UIColor grayColor];
        
        m_On = YES;
    }
    else{;}*/
    
    //[m_Target performSelector:m_Action withObject:self withObject:[NSNumber numberWithBool:m_On]];
    [m_Target performSelector:m_Action withObject:self withObject:[NSNumber numberWithBool:m_Switch.on]];
}

-(void)SetValue:(BOOL)value
{
    /*if (value)
    {
        m_RightBtn.enabled = YES;
        m_LeftBtn.enabled  = NO;
        
        m_RightBtn.titleLabel.textColor = [UIColor blackColor];
        //m_LeftBtn.titleLabel.textColor  = [UIColor grayColor];
    }
    else
    {
        m_RightBtn.enabled = NO;
        m_LeftBtn.enabled  = YES;
        
        m_LeftBtn.titleLabel.textColor  = [UIColor blackColor];
        //m_RightBtn.titleLabel.textColor = [UIColor grayColor];
    }
    
    m_On = value;*/
    
    [m_Switch setOn:value];
}

-(void)AddSwitchTarget:(id)target Selector:(SEL)selector
{
    m_Target = target;
    m_Action = selector;
}

@end


//======================================================================================================================

@implementation COptionView

@synthesize delegate;
@synthesize m_Swith1;
@synthesize m_Swith2;
@synthesize m_Swith3;
@synthesize m_Swith4;
@synthesize m_Swith5;
@synthesize m_Swith6;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        //self.backgroundColor = [UIColor colorWithRed:0.95 green:0.94 blue:0.96 alpha:1.0];
        UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        bg.image = [UIImage imageNamed:@"appbg.jpg"];
        [self addSubview:bg];
        [bg release];
        
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        scroll.backgroundColor = [UIColor clearColor];
        scroll.showsVerticalScrollIndicator = NO;
        [self addSubview:scroll];
        [scroll release];

        UIImageView *lbimg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 84, 24)];
        //lb.text = @"  通知设置";
        lbimg.backgroundColor = [UIColor clearColor];
        lbimg.image = [UIImage imageNamed:@"setlinktitle.png"];
        [scroll addSubview:lbimg];
        [lbimg release]; 
        
        UIView *corner = [[UIView alloc] initWithFrame:CGRectMake(10, lbimg.frame.origin.y+lbimg.frame.size.height+10, self.frame.size.width-15, 3*ROW_HEIGHT+8)];
        corner.backgroundColor = [UIColor clearColor];
        [scroll addSubview:corner];
        [corner release];
        
        lbimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, corner.frame.size.width, corner.frame.size.height)];
        lbimg.image = [UIImage imageNamed:@"setborder.png"];
        [corner addSubview:lbimg];
        [lbimg release];
        
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, corner.frame.size.width, ROW_HEIGHT)];
        lb.text = @"  有人评价我的信息";
        lb.textColor = [UIColor colorWithRed:0.905 green:0.623 blue:0.294 alpha:1.0];
        lb.font = [UIFont boldSystemFontOfSize:15];
        lb.backgroundColor = [UIColor clearColor];
        [corner addSubview:lb];
        [lb release];
        
        m_Swith1 = [[CSwitch alloc] initWithFrame:CGRectMake(corner.frame.size.width-SWITCH_WIDTH, lb.frame.origin.y, SWITCH_WIDTH, ROW_HEIGHT)];
        [m_Swith1 AddSwitchTarget:self Selector:@selector(DidSwitchChanged:Value:)];
        [corner addSubview:m_Swith1];
        [m_Swith1 release];
        
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(-10, lb.frame.origin.y+lb.frame.size.height, self.frame.size.width, 3)];
        //line.backgroundColor = [UIColor lightGrayColor];
        //line.alpha = 0.5;
        line.image = [UIImage imageNamed:@"loginline.png"];
        [corner addSubview:line];
        [line release];
        
        lb = [[UILabel alloc] initWithFrame:CGRectMake(0, line.frame.origin.y+line.frame.size.height, corner.frame.size.width, ROW_HEIGHT)];
        lb.text = @"  我的好友发布信息";
        lb.textColor = [UIColor colorWithRed:0.905 green:0.623 blue:0.294 alpha:1.0];
        lb.font = [UIFont boldSystemFontOfSize:15];
        lb.backgroundColor = [UIColor clearColor];
        [corner addSubview:lb];
        [lb release];
        
        m_Swith2 = [[CSwitch alloc] initWithFrame:CGRectMake(corner.frame.size.width-SWITCH_WIDTH, lb.frame.origin.y, SWITCH_WIDTH, ROW_HEIGHT)];
        [m_Swith2 AddSwitchTarget:self Selector:@selector(DidSwitchChanged:Value:)];
        [corner addSubview:m_Swith2];
        [m_Swith2 release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(-10, lb.frame.origin.y+lb.frame.size.height, self.frame.size.width, 3)];
        //line.backgroundColor = [UIColor lightGrayColor];
        //line.alpha = 0.5;
        line.image = [UIImage imageNamed:@"loginline.png"];
        [corner addSubview:line];
        [line release];
        
        lb = [[UILabel alloc] initWithFrame:CGRectMake(0, line.frame.origin.y+line.frame.size.height, corner.frame.size.width, ROW_HEIGHT)];
        lb.text = @"  接受系统消息";
        lb.textColor = [UIColor colorWithRed:0.905 green:0.623 blue:0.294 alpha:1.0];
        lb.font = [UIFont boldSystemFontOfSize:15];
        lb.backgroundColor = [UIColor clearColor];
        [corner addSubview:lb];
        [lb release];
        
        m_Swith3 = [[CSwitch alloc] initWithFrame:CGRectMake(corner.frame.size.width-SWITCH_WIDTH, lb.frame.origin.y, SWITCH_WIDTH, ROW_HEIGHT)];
        [m_Swith3 AddSwitchTarget:self Selector:@selector(DidSwitchChanged:Value:)];
        [corner addSubview:m_Swith3];
        [m_Swith3 release];
        
        lbimg = [[UIImageView alloc] initWithFrame:CGRectMake(10, corner.frame.origin.y+corner.frame.size.height+10, 84, 24)];
        //lb.text = @"  连接设置";
        lbimg.backgroundColor = [UIColor clearColor];
        lbimg.image = [UIImage imageNamed:@"setnotitle.png"];
        [scroll addSubview:lbimg];
        [lbimg release];
        
        corner = [[UIView alloc] initWithFrame:CGRectMake(10, lbimg.frame.origin.y+lbimg.frame.size.height+10, self.frame.size.width-15, 3*ROW_HEIGHT+5)];
        corner.backgroundColor = [UIColor clearColor];
        [scroll addSubview:corner];
        [corner release];
        
        lbimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, corner.frame.size.width, corner.frame.size.height)];
        lbimg.image = [UIImage imageNamed:@"setborder.png"];
        [corner addSubview:lbimg];
        [lbimg release];
        
        UIImageView *sina = [[UIImageView alloc] initWithFrame:CGRectMake(21, 0, 25, 25)];
        sina.center = CGPointMake(sina.center.x, ROW_HEIGHT/2);
        sina.image = [UIImage imageNamed:@"sina.png"];
        [corner addSubview:sina];
        [sina release];
        
        lb = [[UILabel alloc] initWithFrame:CGRectMake(51, 0, 100, 25)];
        lb.center = CGPointMake(lb.center.x, ROW_HEIGHT/2);
        lb.text = @"新浪微博";
        lb.font = [UIFont boldSystemFontOfSize:15];
        lb.textColor = [UIColor colorWithRed:0.905 green:0.623 blue:0.294 alpha:1.0];
        lb.backgroundColor = [UIColor clearColor];
        [corner addSubview:lb];
        [lb release];
        
        m_Swith4 = [[CSwitch alloc] initWithFrame:CGRectMake(corner.frame.size.width-SWITCH_WIDTH, 0, SWITCH_WIDTH, ROW_HEIGHT)];
        [m_Swith4 AddSwitchTarget:self Selector:@selector(DidSwitchChanged:Value:)];
        [corner addSubview:m_Swith4];
        [m_Swith4 release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(-10, ROW_HEIGHT, self.frame.size.width, 3)];
        //line.backgroundColor = [UIColor lightGrayColor];
        //line.alpha = 0.5;
        line.image = [UIImage imageNamed:@"loginline.png"];
        [corner addSubview:line];
        [line release];
        
        sina = [[UIImageView alloc] initWithFrame:CGRectMake(21, 0, 25, 25)];
        sina.center = CGPointMake(sina.center.x, ROW_HEIGHT+ROW_HEIGHT/2);
        sina.image = [UIImage imageNamed:@"tx.png"];
        [corner addSubview:sina];
        [sina release];
        
        lb = [[UILabel alloc] initWithFrame:CGRectMake(51, 0, 100, 25)];
        lb.center = CGPointMake(lb.center.x, ROW_HEIGHT+ROW_HEIGHT/2);
        lb.text = @"QQ微博";
        lb.font = [UIFont boldSystemFontOfSize:15];
        lb.textColor = [UIColor colorWithRed:0.905 green:0.623 blue:0.294 alpha:1.0];
        lb.backgroundColor = [UIColor clearColor];
        [corner addSubview:lb];
        [lb release];
        
        m_Swith5 = [[CSwitch alloc] initWithFrame:CGRectMake(corner.frame.size.width-SWITCH_WIDTH, ROW_HEIGHT, SWITCH_WIDTH, ROW_HEIGHT)];
        [m_Swith5 AddSwitchTarget:self Selector:@selector(DidSwitchChanged:Value:)];
        [corner addSubview:m_Swith5];
        [m_Swith5 release];
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(-10, 2*ROW_HEIGHT, self.frame.size.width, 3)];
        //line.backgroundColor = [UIColor lightGrayColor];
        //line.alpha = 0.5;
        line.image = [UIImage imageNamed:@"loginline.png"];
        [corner addSubview:line];
        [line release];
        
        sina = [[UIImageView alloc] initWithFrame:CGRectMake(21, 0, 25, 25)];
        sina.center = CGPointMake(sina.center.x, 2*ROW_HEIGHT+ROW_HEIGHT/2);
        sina.image = [UIImage imageNamed:@"tx.png"];
        [corner addSubview:sina];
        [sina release];
        
        lb = [[UILabel alloc] initWithFrame:CGRectMake(51, 0, 100, 25)];
        lb.center = CGPointMake(lb.center.x, 2*ROW_HEIGHT+ROW_HEIGHT/2);
        lb.text = @"人人网";
        lb.font = [UIFont boldSystemFontOfSize:15];
        lb.textColor = [UIColor colorWithRed:0.905 green:0.623 blue:0.294 alpha:1.0];
        lb.backgroundColor = [UIColor clearColor];
        [corner addSubview:lb];
        [lb release];
        
        m_Swith6 = [[CSwitch alloc] initWithFrame:CGRectMake(corner.frame.size.width-SWITCH_WIDTH, 2*ROW_HEIGHT, SWITCH_WIDTH, ROW_HEIGHT)];
        [m_Swith6 AddSwitchTarget:self Selector:@selector(DidSwitchChanged:Value:)];
        [corner addSubview:m_Swith6];
        [m_Swith6 release];
        
        scroll.contentSize = CGSizeMake(frame.size.width, corner.frame.origin.y+corner.frame.size.height+10);
    }
    return self;
}

-(void)dealloc
{
    [super dealloc];
}

-(void)DidSwitchChanged:(CSwitch*)cswitch Value:(NSNumber*)value
{
    long l = ([value boolValue]) ? (1) : (0);
    NSMutableArray *keys = [NSMutableArray arrayWithCapacity:1];
    CKey *key = nil;
    NSString *index = nil;
    
    if (m_Swith1 == cswitch)
    {
        //index = [NSString stringWithString:@"receiveComment"];
        NSNumber *b = [NSNumber numberWithLong:l];
        [CUser DefaultUser].U_DisedOn = b;
        //key = [[CKey alloc] initWithType:1 Value:b Name:@"U_DisedOn"];
    }
    else if (m_Swith2 == cswitch)
    {
        //index = [NSString stringWithString:@"receiveFriendArticle"];
        NSNumber *b = [NSNumber numberWithLong:l];
        [CUser DefaultUser].U_FpubOn = b;
        //key = [[CKey alloc] initWithType:1 Value:b Name:@"U_FpubOn"];
    }
    else if (m_Swith3 == cswitch)
    {
        //index = [NSString stringWithString:@"receiveOS"];
        NSNumber *b = [NSNumber numberWithLong:l];
        [CUser DefaultUser].U_OsOn = b;
        //key = [[CKey alloc] initWithType:1 Value:b Name:@"U_OsOn"];
    }
    else if (m_Swith4 == cswitch)
    {
        index = [NSString stringWithString:@"Sina"];
        NSNumber *b = [NSNumber numberWithLong:l];
        [CUser DefaultUser].U_SinaOn = b;
        key = [[CKey alloc] initWithType:1 Value:b Name:@"U_SinaOn"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:MSG_SINA_SWITCH object:value];
    }
    else if (m_Swith5 == cswitch)
    {
        index = [NSString stringWithString:@"Penguin"];
        NSNumber *b = [NSNumber numberWithLong:l];
        [CUser DefaultUser].U_QqOn = b;
        key = [[CKey alloc] initWithType:1 Value:b Name:@"U_QqOn"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:MSG_QQ_SWITCH object:value];
    }
    else if (m_Swith6 == cswitch)
    {
        index = [NSString stringWithString:@"Renren"];
        NSNumber *b = [NSNumber numberWithLong:l];
        [CUser DefaultUser].U_RenrenOn = b;
        key = [[CKey alloc] initWithType:1 Value:b Name:@"U_RenrenOn"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:MSG_RENREN_SWITCH object:value];
    }
    else {;}
    
    if (key)
    {
        [keys addObject:key];
        [[CUser DefaultUser] UserChangeSwitchValue:index Value:l];
        [CUser UpadteUserInfo:[NSString stringWithFormat:@"U_AID==%ld", [[CUser DefaultUser].U_AID longValue]] Keys:keys];
        [key release];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidReceiveSwitchValueChanged:) name:MSG_CHANGESWITCH_RET object:nil];
    }
}

-(void)SetOptionViewInfo
{
    CUser *user = [CUser DefaultUser];
    
    [m_Swith1 SetValue:[user.U_DisedOn longValue]];
    [m_Swith2 SetValue:[user.U_FpubOn longValue]];
    [m_Swith3 SetValue:[user.U_OsOn longValue]];
    [m_Swith4 SetValue:[user.U_SinaOn longValue]];
    [m_Swith5 SetValue:[user.U_QqOn longValue]];
    [m_Swith6 SetValue:[user.U_RenrenOn longValue]];
}

-(void)DidReceiveSwitchValueChanged:(NSNotification*)notification
{
    NSDictionary *dic = notification.object;
    int nCode = [[dic objectForKey:@"Msg_error"] intValue];
    
    if (0 == nCode)
    {
        //
    }
    else 
    {
        NSMutableArray *keys = [NSMutableArray arrayWithCapacity:1];
        CKey *key = nil;
        
        if ([[dic objectForKey:@"setName"] isEqualToString:@"receiveComment"])
        {
            //[m_Swith1 SetValue:(m_Swith1.m_On) ? (NO) : (YES)];
            [m_Swith1 SetValue:(m_Swith1.m_Switch.on) ? (NO) : (YES)];
            //[CUser DefaultUser].U_DisedOn = [NSNumber numberWithLong:(m_Swith1.m_On) ? (1) : (0)];
            [CUser DefaultUser].U_DisedOn = [NSNumber numberWithLong:(m_Swith1.m_Switch.on) ? (1) : (0)];
            key = [[CKey alloc] initWithType:1 Value:[CUser DefaultUser].U_DisedOn Name:@"U_DisedOn"];
        }
        else if ([[dic objectForKey:@"setName"] isEqualToString:@"receiveFriendArticle"])
        {
            //[m_Swith2 SetValue:(m_Swith2.m_On) ? (NO) : (YES)];
            [m_Swith2 SetValue:(m_Swith2.m_Switch.on) ? (NO) : (YES)];
            //[CUser DefaultUser].U_FpubOn = [NSNumber numberWithLong:(m_Swith2.m_On) ? (1) : (0)];
            [CUser DefaultUser].U_FpubOn = [NSNumber numberWithLong:(m_Swith2.m_Switch.on) ? (1) : (0)];
            key = [[CKey alloc] initWithType:1 Value: [CUser DefaultUser].U_FpubOn Name:@"U_FpubOn"];
        }
        else if ([[dic objectForKey:@"setName"] isEqualToString:@"receiveOS"])
        {
            //[m_Swith3 SetValue:(m_Swith3.m_On) ? (NO) : (YES)];
            [m_Swith3 SetValue:(m_Swith3.m_Switch.on) ? (NO) : (YES)];
            //[CUser DefaultUser].U_OsOn = [NSNumber numberWithLong:(m_Swith3.m_On) ? (1) : (0)];
            [CUser DefaultUser].U_OsOn = [NSNumber numberWithLong:(m_Swith3.m_Switch.on) ? (1) : (0)];
            key = [[CKey alloc] initWithType:1 Value: [CUser DefaultUser].U_OsOn Name:@"U_OsOn"];
        }
        else if ([[dic objectForKey:@"setName"] isEqualToString:@"Sina"])
        {
            //[m_Swith4 SetValue:(m_Swith4.m_On) ? (NO) : (YES)];
            [m_Swith4 SetValue:(m_Swith4.m_Switch.on) ? (NO) : (YES)];
            //[CUser DefaultUser].U_SinaOn = [NSNumber numberWithLong:(m_Swith4.m_On) ? (1) : (0)];
            [CUser DefaultUser].U_SinaOn = [NSNumber numberWithLong:(m_Swith4.m_Switch.on) ? (1) : (0)];
            key = [[CKey alloc] initWithType:1 Value: [CUser DefaultUser].U_SinaOn Name:@"U_SinaOn"];
        }
        else if ([[dic objectForKey:@"setName"] isEqualToString:@"Penguin"])
        {
            //[m_Swith5 SetValue:(m_Swith5.m_On) ? (NO) : (YES)];
            [m_Swith5 SetValue:(m_Swith5.m_Switch.on) ? (NO) : (YES)];
            //[CUser DefaultUser].U_QqOn = [NSNumber numberWithLong:(m_Swith5.m_On) ? (1) : (0)];
            [CUser DefaultUser].U_QqOn = [NSNumber numberWithLong:(m_Swith5.m_Switch.on) ? (1) : (0)];
            key = [[CKey alloc] initWithType:1 Value: [CUser DefaultUser].U_QqOn Name:@"U_QqOn"];
        }
        else if ([[dic objectForKey:@"setName"] isEqualToString:@"U_RenrenOn"])
        {
            //[m_Swith6 SetValue:(m_Swith6.m_On) ? (NO) : (YES)];
            [m_Swith6 SetValue:(m_Swith6.m_Switch.on) ? (NO) : (YES)];
            //[CUser DefaultUser].U_RenrenOn = [NSNumber numberWithLong:(m_Swith6.m_On) ? (1) : (0)];
            [CUser DefaultUser].U_RenrenOn = [NSNumber numberWithLong:(m_Swith6.m_Switch.on) ? (1) : (0)];
            key = [[CKey alloc] initWithType:1 Value: [CUser DefaultUser].U_RenrenOn Name:@"U_RenrenOn"];
        }
        else {;}
        
        [keys addObject:key];
        [CUser UpadteUserInfo:[NSString stringWithFormat:@"U_AID==%ld", [[CUser DefaultUser].U_AID longValue]] Keys:keys];
        [key release];
    }
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_CHANGESWITCH_RET object:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
